#include "VSSamplerState.h"
#include "VSShader.h"
#include "VSMaterial.h"
#include "VSGraphicInclude.h"
#include "VSResourceManager.h"
#include "VSStream.h"
using namespace VSEngine2;
IMPLEMENT_RTTI(VSSamplerState,VSBind)
VSPointer<VSSamplerState> VSSamplerState::Default;
BEGIN_ADD_PROPERTY(VSSamplerState,VSBind)
REGISTER_PROPERTY(m_SamplerDesc,SamplerDesc,VSProperty::F_SAVE_LOAD_CLONE)
END_ADD_PROPERTY
IMPLEMENT_INITIAL_BEGIN(VSSamplerState)
ADD_INITIAL_FUNCTION_WITH_PRIORITY(InitialDefaultState)
ADD_TERMINAL_FUNCTION(TerminalDefaultState)
IMPLEMENT_INITIAL_END
bool VSSamplerState::InitialDefaultState()
{
	VSSamplerDesc SamplerDesc;
	Default = VSResourceManager::CreateSamplerState(SamplerDesc);
	if(!Default)
		return 0;
	return 1;
}
bool VSSamplerState::TerminalDefaultState()
{
	Default = NULL;
	return 1;
}
bool VSSamplerState::OnLoadResource(VSResourceIdentifier *&pID)
{
	if(!m_pUser)
		return 0;
	if(!m_pUser->OnLoadSamplerState(this,pID))
		return 0;
	return 1;
}
bool VSSamplerState::OnReleaseResource(VSResourceIdentifier *pID)
{
	if(!m_pUser)
		return 0;
	if(!m_pUser->OnReleaseSamplerState(pID))
		return 0;
	return 1;
}
VSSamplerState::VSSamplerState()
{
	m_bIsStatic = 1;
}
VSSamplerState::~VSSamplerState()
{
	ReleaseResource();
}
unsigned int VSSamplerState::DiskUse()const
{
	unsigned int Use = VSBind::DiskUse();

	Use += m_SamplerDesc.DiskUse();

	return Use;
}
bool VSSamplerState::Save(VSStream & rStream,unsigned int &iSaveUse)const
{
	if(!VSObject::Save(rStream,iSaveUse))
		return false;


	if (!m_SamplerDesc.Save(rStream,iSaveUse))
	{
		return 0;
	}
	return 1;
}
bool VSSamplerState::Load(VSStream & rStream,unsigned int &iSaveUse)
{

	if(!VSObject::Load(rStream,iSaveUse))
		return false;


	if(!m_SamplerDesc.Load(rStream,iSaveUse))
		return 0;

	return 1;
}
bool VSSamplerState::Clone(const VSObject *pObject,unsigned int uiType)
{
	const VSSamplerState* Temp = DynamicCast<VSSamplerState>(pObject); 
	if(!Temp)
		return 0;

	if(VSBind::Clone(pObject,uiType))
	{

		m_SamplerDesc = Temp->m_SamplerDesc;


		return 1;
	}
	else
	{
		return 0;

	}
}
bool VSSamplerState::Link(VSStream & rStream)
{
	if(!VSObject::Link(rStream))
		return 0;

	return 1;
}
bool VSSamplerState::Register(VSStream & rStream)const
{
	if (rStream.IsObjectRegister(this))
	{
		return 1;
	}
	if(!VSBind::Register(rStream))
		return 0;


	return 1;
}
IMPLEMENT_RTTI(VSSamplerDesc,VSObject)
BEGIN_ADD_PROPERTY(VSSamplerDesc,VSObject)
REGISTER_PROPERTY(m_uiMag,Mag,VSProperty::F_SAVE_LOAD_CLONE)
REGISTER_PROPERTY(m_uiMin,Min,VSProperty::F_SAVE_LOAD_CLONE)
REGISTER_PROPERTY(m_uiMip,Mip,VSProperty::F_SAVE_LOAD_CLONE)
REGISTER_PROPERTY(m_uiMipLevel,MipLevel,VSProperty::F_SAVE_LOAD_CLONE)
REGISTER_PROPERTY(m_uiAniLevel,AniLevel,VSProperty::F_SAVE_LOAD_CLONE)
REGISTER_PROPERTY(m_uiCoordU,CoordU,VSProperty::F_SAVE_LOAD_CLONE)
REGISTER_PROPERTY(m_uiCoordV,CoordV,VSProperty::F_SAVE_LOAD_CLONE)
REGISTER_PROPERTY(m_uiCoordW,CoordW,VSProperty::F_SAVE_LOAD_CLONE)
REGISTER_PROPERTY(m_BorderColor,BorderColor,VSProperty::F_SAVE_LOAD_CLONE)
END_ADD_PROPERTY
IMPLEMENT_INITIAL_BEGIN(VSSamplerDesc)
IMPLEMENT_INITIAL_END
bool VSSamplerDesc::Save(VSStream & rStream,unsigned int &iSaveUse)const
{


	if(!rStream.Write(&m_uiMag,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Write(&m_uiMin,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Write(&m_uiMip,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);



	if(!rStream.Write(&m_uiMipLevel,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Write(&m_uiAniLevel,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Write(&m_uiCoordU,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Write(&m_uiCoordV,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Write(&m_uiCoordW,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Write(&m_BorderColor,sizeof(VSColorRGBA)))
		return false;
	iSaveUse += sizeof(VSColorRGBA);
	return true;
}
bool VSSamplerDesc::Load(VSStream & rStream,unsigned int &iSaveUse)
{

	if(!rStream.Read(&m_uiMag,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Read(&m_uiMin,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Read(&m_uiMip,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);



	if(!rStream.Read(&m_uiMipLevel,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Read(&m_uiAniLevel,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Read(&m_uiCoordU,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Read(&m_uiCoordV,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Read(&m_uiCoordW,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Read(&m_BorderColor,sizeof(VSColorRGBA)))
		return false;
	iSaveUse += sizeof(VSColorRGBA);
	return true;
}